package pms.dao.impl;


import pms.dao.CustomerDao;
import pms.entity.Customer;
import pms.until.JDBCTools;

import java.sql.ResultSet;
import java.sql.SQLException;

public class CustomerDaoImpl implements CustomerDao {
    private JDBCTools tools=new JDBCTools();
    @Override
    public Customer queryCustomerNameByID(Integer customerid) {
        Customer customer=null;
        ResultSet rs=tools.query("select supplier_name from t_supplier where supplier_id=?",customerid);

        try {
            if (rs.next()){
                customer=new Customer();
                customer.setCustomer_name(rs.getString("supplier_name"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            tools.clossAll();
        }
        return customer;
    }
    @Override
    public boolean addCustomerForLeave(Customer customer) {
        int i= tools.update("insert into t_customer (customer_name,customer_tel,customer_level) values(?,?,?)",
                customer.getCustomer_name(),customer.getCustomer_tel(),customer.getCustomer_level());
        return i>0;
    }

    @Override
    public Customer queryCustomerById(int customerid) {
        Customer customer=null;
        ResultSet rs=tools.query("select * from t_customer where customer_id=?",customerid);
        try {
            while(rs.next()){
                customer=new Customer();
                customer.setCustomer_id(rs.getInt("customer_id"));
                customer.setCustomer_name(rs.getString("customer_name"));
                customer.setCustomer_tel(rs.getString("customer_tel"));
                customer.setCustomer_level(rs.getString("customer_level"));

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            tools.clossAll();
        }

        return customer;

    }

    @Override
    public Customer queryCustomerBytel(String customertel) {
        Customer customer=null;
        ResultSet rs=tools.query("select * from t_customer where customer_tel=?",customertel);
        try {
            while(rs.next()){
                customer=new Customer();
                customer.setCustomer_id(rs.getInt("customer_id"));
                customer.setCustomer_name(rs.getString("customer_name"));
                customer.setCustomer_tel(rs.getString("customer_tel"));
                customer.setCustomer_level(rs.getString("customer_level"));

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            tools.clossAll();
        }

        return customer;

    }

    @Override
    public boolean queryCustomerForLeave(Customer customer) {
        boolean flag=false;
        String sql="select * from t_customer where  customer_name=? and customer_tel=? and customer_level=?";
        ResultSet rs= tools.query(sql,customer.getCustomer_name(),customer.getCustomer_tel(),customer.getCustomer_level());
        try {
            if(rs.next()){
                flag=true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            tools.clossAll();
        }
        return flag;
    }
}
